package com.zhang.network.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.network.mapper.NetworkMapper;
import com.zhang.network.mapper.NetworkUserMapper;
import com.zhang.network.model.Network;
import com.zhang.network.model.NetworkUser;
import com.zhang.network.model.network.NetWorkQueryDTO;
import com.zhang.network.model.network.NetworkVo;
import com.zhang.network.service.NetworkService;
import com.zhang.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhang
 * @since 2021-04-03
 */
@Service
public class NetworkServiceImpl extends ServiceImpl<NetworkMapper, Network> implements NetworkService {

    @Resource
    private NetworkUserMapper networkUserMapper;

    @Override
    public Page<Network> pageNetwork(NetWorkQueryDTO queryDTO) {
        Page<Network> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        QueryWrapper<Network> wrapper = new QueryWrapper<>();
        // 管理员id == 当前用户id 的是当前用户的人脉圈
        wrapper.eq("user_id", queryDTO.getUserId());
        if (StringUtils.isNotBlank(queryDTO.getTitle())) {
            wrapper.like("title", queryDTO.getTitle());
        }
        if (StringUtils.isNotBlank(queryDTO.getType())) {
            wrapper.eq("type", queryDTO.getType());
        }
        return baseMapper.selectPage(page, wrapper);
    }

    @Override
    public Page<Network> pageInNetwork(NetWorkQueryDTO queryDTO) {
        Page<Network> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        QueryWrapper<NetworkUser> networkUserQueryWrapper = new QueryWrapper<>();
        // userId = 当前用户id 说明当前用户所在人脉圈，且isAdmin = 0 非管理的人脉圈
        networkUserQueryWrapper.eq("user_id", queryDTO.getUserId()).eq("is_admin", 0);
        List<Integer> networkIds = networkUserMapper.selectList(networkUserQueryWrapper).stream().map(item -> item.getNetworkId()).collect(Collectors.toList());
        QueryWrapper<Network> wrapper = new QueryWrapper<>();
        if (networkIds.size() != 0) {
            wrapper.in("id", networkIds);
        } else {
            wrapper.eq("id", Integer.MAX_VALUE);
        }
        if (StringUtils.isNotBlank(queryDTO.getTitle())) {
            wrapper.like("title", queryDTO.getTitle());
        }
        if (StringUtils.isNotBlank(queryDTO.getType())) {
            wrapper.eq("type", queryDTO.getType());
        }
        baseMapper.selectPage(page, wrapper);
        return page;
    }

    @Override
    public List<String> getNetworkTypes() {
        List<String> types = baseMapper.selectList(null).stream().map(Network::getType).distinct().collect(Collectors.toList());
        /*List<String> types = new ArrayList<>();
        types.add("同事圈");
        types.add("朋友圈");
        types.add("亲友圈");*/
        return types;
    }

    @Override
    public boolean insertNetwork(Network network) {
        return baseMapper.insert(network) != 0;
    }

    @Override
    public boolean modifyNetwork(Network network) {
        return baseMapper.updateById(network) != 0;
    }

    /**
     * 根据人脉圈 id 删除人脉圈以及人脉圈内好友关联
     *
     * @param id network id
     * @return 是否删除成功
     */
    @Override
    public boolean deleteNetwork(Integer id) {
        return baseMapper.deleteById(id) != 0 || networkUserMapper.delete(new QueryWrapper<NetworkUser>().eq("network_id", id)) != 0;
    }
}
